var app = getApp();
const DrainageScene = require('../../../../utils/drainage_scene.js')

Component({
  properties: {
    params: {
      type: null
    },
    data: {
      type: null
    }
  },
  data: {
    language: '',
    coupon_list: []
  }, // 私有数据，可用于模板渲染

  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {
      let language = wx.getStorageSync('langIndex');
      this.setData({ 'language': language.en});
    },
    moved() {},
    detached() {},
  },

  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached() {

  }, // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready() {
    if (this.data.params.addmethod == "0") {
      this.autoGetData();
    } else {
      this.setData({
        coupon_list: this.data.data
      });
    }
  },

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show() {},
    hide() {},
    resize() {},
  },

  methods: {
    autoGetData() {
      var that = this;
      var urlStr = app.getNetAddresss("coupon.member-coupon.coupons-for-designer");
      app._getNetWork({
        showToastIn: false,
        url: urlStr,
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              coupon_list: res.data.data,
            });
          }
        },
        fail: function(res) {

        }
      });
    },
    selectedcoupon(e) {
      let isBindDrainageScene = DrainageScene.bindDrainageScene({
        scene: 3,
        title: '领取优惠券'
      });
      if(!isBindDrainageScene) return;//爆客码绑定场景

      let index = e.currentTarget.dataset.index;
      let item = e.currentTarget.dataset.item;
      if (item.api_availability == 3) {
        return;
      }
      //领取
      var that = this;
      var urlStr = app.getNetAddresss("coupon.member-coupon.get-coupon");
      urlStr += '&coupon_id=' + item.coupon_id;
      app._getNetWork({
        showToastIn: false,
        url: urlStr,
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            var temp = res.data;
            that.setData({
              ['coupon_list[' + index + ']']: temp
            });
            wx.showToast({
              icon: 'none',
              title: '优惠券领取成功',
              duration: 1500
            });
          } else {
            if(res.data.reason){
              wx.showModal({
                title:res.msg,
                content:res.data.reason,
                showCancel:false
              });
            }else{
              wx.showToast({
                icon: 'none',
                title: res.msg,
                duration: 1000
              });
            }
          }
        },
        fail: function(res) {

        }
      });
    },
  },

});
